package com.example.im.mvp.base

import android.Manifest
import android.app.Activity
import android.content.Context
import androidx.lifecycle.LifecycleOwner
import com.example.im.lifecycle.LifecyclePresenter
import com.tbruyelle.rxpermissions2.RxPermissions
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable


open class BasePresenter<V : BaseView>(private var mContext: Context, private var mView: V?) :
    LifecyclePresenter {

    var disposable = CompositeDisposable()

    /**
     * View是否绑定
     *
     * @return
     */
    val isViewAttached: Boolean get() = mView != null

    /**
     * 绑定view，初始化中调用该方法
     *
     * @param view view
     */
    fun attachView(view: V) {
        this.mView = view
    }

    /**
     * 解除绑定view，onDestroy中调用
     */
    open fun detachView() {
        this.mView = null
    }

    /**
     * 获取已绑定view
     * */
    fun getView(): V {
        return mView!!
    }

    /**
     * 获取上下文
     * */
    fun getContext(): Context {
        return mContext
    }

    /**
     * 权限申请
     */
    fun applyPermissions(activity: Activity): Observable<Boolean> {
        return RxPermissions(activity).request(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA
        )
    }

    /**********实现LifecyclePresenter接口 同步activity,fragment生命周期 **********/

    override fun onCreate(owner: LifecycleOwner) {

    }

    override fun onDestroy(owner: LifecycleOwner) {

    }

    override fun onAny(owner: LifecycleOwner) {

    }
}
